package com.mis.pos.repository;

import com.mis.pos.entity.InventoryEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

/**
 * @Description 库存DAO 结合Querydsl
 * @Author yuxiang
 * @CreatedDate 2019/11/26 9:31
 */
@Repository
public interface InventoryRepository extends JpaRepository<InventoryEntity,Long>, JpaSpecificationExecutor<InventoryEntity> {

    /**
     * 库存减少
     * @param itemId
     * @param number
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "update inventory it set it.amount=it.amount-?2 where item_id=?1",nativeQuery = true)
    int updateInventoryNumberByItemId(Long itemId, Long number);

    /**
     * 库存增加
     * @param itemId
     * @param number
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "update inventory it set it.amount=it.amount+?2 where item_id=?1",nativeQuery = true)
    int addInventoryNumberByItemId(Long itemId, Long number);
}
